

package com.hazelcast.internal.monitor.impl;

import com.hazelcast.internal.metrics.Probe;
import com.hazelcast.partition.LocalReplicationStats;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;

import static com.hazelcast.internal.metrics.MetricDescriptorConstants.*;
import static java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater;

public class LocalReplicationStatsImpl implements LocalReplicationStats {

    private static final AtomicLongFieldUpdater<LocalReplicationStatsImpl> FULL_PARTITION_REPLICATION_COUNT = newUpdater(LocalReplicationStatsImpl.class, "fullPartitionReplicationCount");
    private static final AtomicLongFieldUpdater<LocalReplicationStatsImpl> DIFF_PARTITION_REPLICATION_COUNT = newUpdater(LocalReplicationStatsImpl.class, "diffPartitionReplicationCount");
    private static final AtomicLongFieldUpdater<LocalReplicationStatsImpl> FULL_PARTITION_REPLICATION_RECORDS_COUNT = newUpdater(LocalReplicationStatsImpl.class, "fullPartitionReplicationRecordsCount");
    private static final AtomicLongFieldUpdater<LocalReplicationStatsImpl> DIFF_PARTITION_REPLICATION_RECORDS_COUNT = newUpdater(LocalReplicationStatsImpl.class, "diffPartitionReplicationRecordsCount");

    @Probe(name = MAP_METRIC_FULL_PARTITION_REPLICATION_COUNT)
    private volatile long fullPartitionReplicationCount;
    @Probe(name = MAP_METRIC_DIFF_PARTITION_REPLICATION_COUNT)
    private volatile long diffPartitionReplicationCount;
    @Probe(name = MAP_METRIC_FULL_PARTITION_REPLICATION_RECORDS_COUNT)
    private volatile long fullPartitionReplicationRecordsCount;
    @Probe(name = MAP_METRIC_DIFF_PARTITION_REPLICATION_RECORDS_COUNT)
    private volatile long diffPartitionReplicationRecordsCount;

    @Override
    public long getDifferentialReplicationRecordCount() {
        return diffPartitionReplicationRecordsCount;
    }

    @Override
    public long getFullReplicationRecordCount() {
        return fullPartitionReplicationRecordsCount;
    }

    @Override
    public long getDifferentialPartitionReplicationCount() {
        return diffPartitionReplicationCount;
    }

    @Override
    public long getFullPartitionReplicationCount() {
        return fullPartitionReplicationCount;
    }

    public void incrementFullPartitionReplicationCount() {
        FULL_PARTITION_REPLICATION_COUNT.incrementAndGet(this);
    }

    public void incrementDiffPartitionReplicationCount() {
        DIFF_PARTITION_REPLICATION_COUNT.incrementAndGet(this);
    }

    public void incrementFullPartitionReplicationRecordsCount(long delta) {
        FULL_PARTITION_REPLICATION_RECORDS_COUNT.addAndGet(this, delta);
    }

    public void incrementDiffPartitionReplicationRecordsCount(long delta) {
        DIFF_PARTITION_REPLICATION_RECORDS_COUNT.addAndGet(this, delta);
    }

    @Override
    public String toString() {
        return "LocalReplicationStats{" + "fullPartitionReplicationCount=" + fullPartitionReplicationCount + ", differentialPartitionReplicationCount=" + diffPartitionReplicationCount + ", fullPartitionReplicationRecordsCount=" + fullPartitionReplicationRecordsCount + ", differentialPartitionReplicationRecordsCount=" + diffPartitionReplicationRecordsCount + '}';
    }
}
